package guoanmaker.provider.business.service;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.provider.business.model.ProvideRoom;
import guoanmaker.provider.business.model.ProvideSpacefacilities;
import guoanmaker.provider.business.modelnobind.ProvideRoomMove;
import guoanmaker.provider.business.repository.ProvideRoomRepository;
import guoanmaker.provider.business.repository.ProvideSpacefacilitiesRepository;

@Service
public class ProvideRoomService implements ProvideRoomServiceInterface{
	
	@Autowired
	private ProvideRoomRepository roomRepository;
	
	@Autowired
	private ProvideSpacefacilitiesRepository provideSpacefacilitiesRepository;
	
	@PersistenceContext
	private EntityManager manager;

	/***
	 * 添加修改空间
	 * @param room 空间对象
	 */
	@Override
	@Transactional
	public ProvideRoom addProvideRoom(ProvideRoom room) {
		room.setRoomTime(new Date());
		room=roomRepository.save(room);
		return room;
	}

	@Transactional
	public Map<String,Object> selectRoomKeyNameBusinessIdStauts(String businessId, String keyName, Integer roomState,Integer page, Integer size) {
		keyName=keyName==null?"":keyName;
		Map<String,Object> map=new HashMap<String,Object>();
		String jpql="select new guoanmaker.provider.business.modelnobind.ProvideRoomMove(rm.id,rm.roomName,rm.roomCodes,rm.describeAsk,rm.address,"
				+ "rm.roomTime,rm.roomState,"
				+ "rm.auditOpinion,rm.violationReasons) "
				+ "from ProvideRoom rm left join rm.business b where b.id=:businessId and rm.roomName like :keyName ";
		if(roomState==-1){
			jpql+=" and  rm.roomState in(1,2)  order by rm.roomTime desc";
		}
		else{
			jpql+=" and rm.roomState=:roomState order by rm.roomTime desc";
		}
		Query query = manager.createQuery(jpql).setParameter("businessId", businessId).setParameter("keyName", "%"+keyName+"%");
		if(roomState!=-1){
			query.setParameter("roomState", roomState);
		}
		int count=query.getResultList().size();
		List<ProvideRoom> list=query.setMaxResults(size)
				.setFirstResult(page*size)
				.getResultList();
		map.put("count", count);
		map.put("list", list);
		if(roomState==3 || roomState==5){
			jpql = "UPDATE ProvideRoom p set p.roomUnread=1 where p.roomState =:roomState and p.business.id=:businessId"; 
			Query query1 = manager.createQuery(jpql).setParameter("businessId", businessId).setParameter("roomState", roomState); 
			query1.executeUpdate(); 
		}
		String jpql2="SELECT new guoanmaker.provider.business.modelnobind.ProvideRoomMove(c.roomState,c.roomUnread,COUNT(c.id)) FROM ProvideRoom c left join c.business b WHERE "
				+ "b.id =:businessId AND c.roomUnread = 0 and c.roomState in (3,5) GROUP BY c.roomState";
		Query querys = manager.createQuery(jpql2).setParameter("businessId", businessId);
		List lists=querys.getResultList();
		map.put("roomUnread", lists);
		return map;
	}

	@Transactional
	public int deleteRoomId(String id) {
		String jpql = "UPDATE ProvideRoom p set p.roomState=6 where p.id =:id"; 
		Query query = manager.createQuery(jpql); 
		query.setParameter("id",id); 
		return query.executeUpdate(); 
	}

	@Transactional
	public int closeOpenRoom(String id,Integer roomState) {
		String jpql = "UPDATE ProvideRoom p set p.roomState=:roomState where p.id =:id"; 
		Query query = manager.createQuery(jpql);
		query.setParameter("id",id); 
		query.setParameter("roomState",roomState);
		int a=query.executeUpdate(); 
		if(a!=0){
			if(roomState==4){
				String jpql1 = "UPDATE ProvideCommodity p set p.commodityState=:commodityState where p.romm.id =:id"; 
				query = manager.createQuery(jpql1);
				query.setParameter("id",id); 
				query.setParameter("commodityState",4);
				a=query.executeUpdate(); 
			}
		}
		return a;
	}

	@Override
	public List<ProvideRoom> selectRoomListStauts(Integer roomState,String businessId) {
		String jpql="select new guoanmaker.provider.business.modelnobind.ProvideRoomMove(rm.id,rm.roomName) "
				+ "from ProvideRoom rm where rm.roomState=:roomState and rm.business.id=:businessId";
		Query query = manager.createQuery(jpql);
		List<ProvideRoom> list=query.setParameter("roomState", roomState).setParameter("businessId", businessId).getResultList();
		return list;
	}

	@Override
	public ProvideRoomMove selectRoomById(String id) {
		String jpql="select new guoanmaker.provider.business.modelnobind.ProvideRoomMove(rm.id,rm.roomName,rm.describeAsk,rm.address,rm.lat,rm.lng,"
				+ "rm.roomTime,rm.roomState,rm.auditOpinion,rm.violationReasons,rm.spaceDetail,rm.province,rm.city,rm.area,rm.complement,"
				+ "rm.roomImgOne,rm.roomImgTwo,rm.roomImgThree,rm.roomImgFour,rm.roomImgFive,rm.roomlogo)"
				+ " from ProvideRoom rm where rm.id=:id";
		Query query = manager.createQuery(jpql);
		ProvideRoomMove provideRoomMove=null;
		String complement=null;
		List lists=query.setParameter("id", id).getResultList();
		if(lists!=null&&lists.size()!=0){
			provideRoomMove=(ProvideRoomMove) lists.get(0);
			complement=provideRoomMove.getComplement();
		}
		List<String> list=null;
		List<ProvideSpacefacilities> spacefacilities=null;
		if(complement!=null&&!complement.equals("")){
			list=Arrays.asList(complement.split(","));
			spacefacilities=provideSpacefacilitiesRepository.findByIdIn(list);
			provideRoomMove.setSpacefacilities(spacefacilities);
		}
		return provideRoomMove;
	}

	@Override
	public void updateRoomById(ProvideRoom provideRoom) {
		ProvideRoom pm=roomRepository.findOne(provideRoom.getId());
		pm.setRoomName(provideRoom.getRoomName());
		pm.setDescribeAsk(provideRoom.getDescribeAsk());
		pm.setProvince(provideRoom.getProvince());
		pm.setCity(provideRoom.getCity());
		pm.setArea(provideRoom.getArea());
		pm.setLat(provideRoom.getLat());
		pm.setLng(provideRoom.getLng());
		pm.setAddress(provideRoom.getAddress());
		pm.setComplement(provideRoom.getComplement());
		pm.setRoomlogo(provideRoom.getRoomlogo());
		pm.setRoomImgOne(provideRoom.getRoomImgOne());
		pm.setRoomImgTwo(provideRoom.getRoomImgTwo());
		pm.setRoomImgThree(provideRoom.getRoomImgThree());
		pm.setRoomImgFour(provideRoom.getRoomImgFour());
		pm.setRoomImgFive(provideRoom.getRoomImgFive());
		pm.setSpaceDetail(provideRoom.getSpaceDetail());
		pm.setRoomTime(new Date());
		pm.setRoomState(1);
		roomRepository.save(pm);
		return ;
	}
	@Transactional
	public int createRoomCodes(ProvideRoom room){
		String jpql1="select new guoanmaker.provider.business.modelnobind.ProvideRoomMove(rm.roomCode)"
				+ " from ProvideRoom rm where rm.id=:id";
		Query query1 = manager.createQuery(jpql1);
		query1.setParameter("id", room.getId());
		ProvideRoomMove rooms=(ProvideRoomMove) query1.getResultList().get(0);
		int code=rooms.getRoomCode();
		String jpql = "UPDATE ProvideRoom p set p.roomCodes=:roomCodes where p.id =:id"; 
		Query query = manager.createQuery(jpql); 
		query.setParameter("id",room.getId()); 
		query.setParameter("roomCodes","S"+code); 
		return query.executeUpdate(); 
	}

	@Override
	public void deleteRoom(String id) {
		// TODO Auto-generated method stub
		roomRepository.delete(id);
	}

}
